package cn.doctorpda.conference.ui.search;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.event.GetMessage;
import cn.doctorpda.conference.event.GetSearchKeyMessage;
import cn.doctorpda.conference.event.GetSearchKeyTopic;
import cn.doctorpda.conference.event.ToNews;
import cn.doctorpda.conference.ui.BaseFragment;
import cn.doctorpda.conference.ui.common.DetailFragment;
import cn.doctorpda.conference.utils.T;

/**
 * Created by huangjinlong on 2016/5/17.
 */
public class SearchFragment extends BaseFragment implements View.OnClickListener {

    private String[] titles = new String[]{"会议", "视频", "PPT", "文献", "资讯"};
    private TabLayout tab_layout;
    private ViewPager info_viewpager;
    private List<Fragment> fragments = new ArrayList<>();
    private FragmentPagerAdapter mPagerAdater;

    private EditText et_search_key;
    private String toNews = "";
    private View view;
    private TextView tv_to_search;

    public static SearchFragment newInstance(String toNews) {
        SearchFragment fragment = new SearchFragment();
        Bundle bundle = new Bundle();
        bundle.putString("toNews", toNews);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toNews = getArguments().getString("toNews");
//        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onResume() {
        super.onResume();

        if (toNews.equals("toNews")) {
            info_viewpager.setCurrentItem(4);
        }
//        T.showShort(getContext(),"慢待你"+toNews);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view != null) {

            return view;
        }

        view = inflater.inflate(R.layout.fragment_search, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        fragments.add(SearchConFragment.newInstance());
        fragments.add(SearchCommonFragment.newInstance("*", "con_video"));
        fragments.add(SearchCommonFragment.newInstance("*", "con_ppt"));
        fragments.add(SearchCommonFragment.newInstance("*", "con_document"));
        fragments.add(SearchTopicFragment.newInstance());

        mPagerAdater = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        };
        info_viewpager.setAdapter(mPagerAdater);
        info_viewpager.setCurrentItem(0);
        info_viewpager.setOffscreenPageLimit(4);
        tab_layout.setupWithViewPager(info_viewpager);
        tab_layout.setTabMode(TabLayout.MODE_FIXED);
    }

    private void initView(View view) {

        view.findViewById(R.id.imv_back).setOnClickListener(this);

        tab_layout = (TabLayout) view.findViewById(R.id.tab_layout);
        info_viewpager = (ViewPager) view.findViewById(R.id.info_viewpager);

        et_search_key = (EditText) view.findViewById(R.id.et_search_key);
        tv_to_search = (TextView) view.findViewById(R.id.btn_to_search);
        tv_to_search.setOnClickListener(this);

        //搜索输入完成监听
        et_search_key.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handle = false;
                String strInput;
                switch (actionId) {
                    case EditorInfo.IME_ACTION_DONE:

                        strInput = et_search_key.getText().toString().trim();
                        if (strInput.isEmpty()) {
                            strInput = "*";
                        }
                        EventBus.getDefault().post(new GetSearchKeyMessage(strInput));
                        break;
                }


                return false;
            }
        });
        et_search_key.addTextChangedListener(textWatcher);


    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String strChange;
            strChange = et_search_key.getText().toString().trim();
            if (strChange.isEmpty()) {
                strChange = "*";
            }
            EventBus.getDefault().post(new GetSearchKeyMessage(strChange));
        }
    };


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.imv_back:
                getActivity().finish();
                break;
            case R.id.btn_to_search:
                InputMethodManager imm = (InputMethodManager)getContext().getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_search_key.getWindowToken(), 0);

                String strChange;
                strChange = et_search_key.getText().toString().trim();
                if (strChange.isEmpty()) {
                    strChange = "*";
                }
                EventBus.getDefault().post(new GetSearchKeyMessage(strChange));


                break;

        }

    }

    @Override
    public String getPageName() {
        return "SearchFragment";
    }
}
